﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using MockPrjTeam2.DAO;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Biz
{
    public class ServiceBiz
    {
        readonly ServiceDao _serviceDao = new ServiceDao();
        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public List<Service> GetAll()
        {
            return _serviceDao.GetAll();
        }

        /// <summary>
        /// Gets the service by id.
        /// </summary>
        /// <param name="serviceId">The service id.</param>
        /// <returns></returns>
        public List<Service> GetById(int serviceId)
        {
            return _serviceDao.GetById(serviceId);
        }

        /// <summary>
        /// Adds the services.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <returns></returns>
        public bool AddServices(Service service)
        {
            return _serviceDao.AddServices(service);
        }


        /// <summary>
        /// Updates the services.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="serviceId">The service id.</param>
        /// <param name="serviceName">Name of the service.</param>
        /// <returns></returns>
        public bool UpdateServices(Service service, int serviceId, string serviceName)
        {
            if (!IsServiceNameExist(serviceId,serviceName))
            {
                return _serviceDao.UpdateServices(service);
            }
            return false;
        }

        /// <summary>
        /// Starts the with by names.
        /// </summary>
        /// <param name="startWith">The start with.</param>
        /// <returns></returns>
        public DataTable StartWithByNames(string startWith, int flag)
        {
            return _serviceDao.StartWithByNames(startWith,flag);
        }

        /// <summary>
        /// Sets the active service.
        /// </summary>
        /// <param name="serviceId">The service id.</param>
        /// <param name="status">if set to <c>true</c> [status].</param>
        /// <returns></returns>
        public bool SetActiveService(int serviceId, bool status)
        {
            return _serviceDao.SetActiveService(serviceId, status);
        }

        /// <summary>
        /// Gets the by in active.
        /// </summary>
        /// <returns></returns>
        public DataTable GetByInActive()
        {
            return _serviceDao.GetByInActive();
        }

        /// <summary>
        /// Determines whether [is service name exist] [the specified service id].
        /// </summary>
        /// <param name="serviceId">The service id.</param>
        /// <param name="serviceName">Name of the service.</param>
        /// <returns>
        ///   <c>true</c> if [is service name exist] [the specified service id]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsServiceNameExist(int serviceId, string serviceName)
        {
            var listService = _serviceDao.IsServiceNameExist(serviceId);
            if (listService != null)
            {
                foreach (var service in listService)
                {
                    if (service.ServiceName.Equals(serviceName))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        internal int IsServiceNameExist(string serviceName)
        {
            return _serviceDao.IsServiceNameExist(serviceName);
        }
    }
}